package com.itheima.pinda.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.itheima.pinda.auth.server.utils.JwtTokenServerUtils;
import com.itheima.pinda.DTO.entity.PdAuthUser;
import com.itheima.pinda.service.PdAuthUserService;
import com.itheima.pinda.Data;
import com.itheima.pinda.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author xsf
 * @since 2021-11-11
 */
@RestController
@RequestMapping("/authority/anno")
@CrossOrigin
public class PdAuthUserController {

    @Autowired
    private JwtTokenServerUtils jwtTokenServerUtils;

    @Autowired
    private PdAuthUserService pdAuthUserService;

    @PostMapping("/login")
    public Result login(@RequestBody PdAuthUser pdAuthUser){


        //通过账号密码查询
      /*  String account = pdAuthUser.getAccount();
        PdAuthUser user = pdAuthUserService.queryUserByName(account);

        if(ObjectUtils.isEmpty(user)){
            //账号或密码错误
            Data data = new Data(null, null, null);
            Result<PdAuthUser> result = new Result<PdAuthUser>(false,data);

        }else if(user.getStatus().equals(0)){
            //该用户被禁用
            Data data = new Data(null, null, null);
            Result<PdAuthUser> result = new Result<PdAuthUser>(false,data);

        }

        //生成jwt令牌
        JwtUserInfo jwtUserInfo = new JwtUserInfo();
        jwtUserInfo.setName(account);
        jwtUserInfo.setOrgId(user.getOrgId());
        jwtUserInfo.setUserId(user.getId());
        jwtUserInfo.setAccount(account);
        jwtUserInfo.setStationId(user.getStationId());*/

//        Token token = jwtTokenServerUtils.generateUserToken(jwtUserInfo, null);


//        JwtUserInfo userInfo = jwtTokenServerUtils.getUserInfo(String.valueOf(token));
        PdAuthUser user=new PdAuthUser();
        user.setAccount("pinda");
        user.setId(3l);
        user.setOrgId(100L);
        user.setStationId(100L);
        Data data = new Data("ehkfjrekjreljioe",user,null);

        Result<PdAuthUser> result = new Result<PdAuthUser>(true,data);


        return result;


    }

    @PostMapping("/queryById")
    public PdAuthUser queryById(@RequestParam String id){
        PdAuthUser pdAuthUser=pdAuthUserService.queryById(id);
        return pdAuthUser;
    }

    @PostMapping("/clientLogin")
    public PdAuthUser clientLogin(@RequestBody PdAuthUser pdAuthUser){
        //通过账号密码查询
        String account = pdAuthUser.getAccount();
        PdAuthUser user = pdAuthUserService.queryUserByName(account);
        if(ObjectUtils.isEmpty(user)){
            //账号或密码错误
            return null;
        }else if(user.getStatus().equals(0)){
            //该用户被禁用
            return null;
        }else if(user.getPassword().equals(pdAuthUser.getPassword())){
            return user;
        }
        return null;

    }
}
